package Spring5.第二章.demo2_3_单例模式;

/**
 * 懒汉式单例模式
 * 特点：只有用到的时候才会进行实例化
 */
public class LazySimpleSingleton {
    private LazySimpleSingleton(){}
    // 静态内存区域
    private static LazySimpleSingleton lazySimpleSingleton = null;

    public static synchronized LazySimpleSingleton getInstance() {
        if (lazySimpleSingleton == null) {
            lazySimpleSingleton = new LazySimpleSingleton();
        }
        return lazySimpleSingleton;
    }

    // 测试
    public static void main(String[] args) {
        Thread thread1 = new Thread(new ExectorThread());
        Thread thread2 = new Thread(new ExectorThread());
        thread1.start();
        thread2.start();
        System.out.println("end");
    }
}

class ExectorThread implements Runnable {

    @Override
    public void run() {
        LazySimpleSingleton lazySimpleSingleton = LazySimpleSingleton.getInstance();
        System.out.println(Thread.currentThread().getName() + ":" + lazySimpleSingleton);
    }
}
